//
//  HRSelectBackGroundMusicView.m
//  Hear
//
//  Created by mac on 2017/4/6.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRSelectBackGroundMusicView.h"
#import "HRSelectMusicCell.h"
#import "HRSelectBackGroundMusicModel.h"
@interface HRSelectBackGroundMusicView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *musicArray;

@end
@implementation HRSelectBackGroundMusicView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = @"选择一个背景音效";
        titleLabel.textColor = CustomColor(34, 34, 34, 1);
        titleLabel.font = CustomUIFont(15);
        [self addSubview:titleLabel];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        
        
        UIButton *closeButton = [UIButton buttonWithType:0];
        [closeButton addTarget:self action:@selector(closeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:closeButton];
        [closeButton setImage:[UIImage imageNamed:@"hear_关闭"] forState:0];
        
        
        
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(100, 80);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.frame collectionViewLayout:layout];
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = YES;
        [collectionView registerClass:[HRSelectMusicCell class] forCellWithReuseIdentifier:@"HRSelectMusicCell"];
        [self addSubview:(self.collectionView = collectionView)];
        
        
        titleLabel.frame = CGRectMake(0, 0, self.width, 20);
        closeButton.frame = CGRectMake(self.width-30, 0, 20, 20);
        collectionView.frame = CGRectMake(0, titleLabel.bottom+10, self.width, self.height-titleLabel.bottom-10);


        
        self.musicArray = [NSMutableArray array];
        
        NSArray *musiceArr = [NSArray arrayWithObjects:@"无音效",@"大雨磅礴",@"钢琴物语",@"海鸥之歌",@"海之胸怀",@"鸟语花香",@"夏日雨纷纷",@"仙境奇侠",@"小溪潺潺",@"吉他抒情曲",@"水之精灵",@"夜的钢琴曲",@"植物大战僵尸", nil];
        
        for (NSString *musicName in musiceArr) {
            HRSelectBackGroundMusicModel *model = [HRSelectBackGroundMusicModel new];
            model.musicName = musicName;
            model.isSelect = NO;
            [self.musicArray addObject:model];
        }
        [self.collectionView reloadData];
        
        
    }
    return self;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.musicArray.count;
}


- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HRSelectMusicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HRSelectMusicCell" forIndexPath:indexPath];
    cell.musicModel = self.musicArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HRSelectBackGroundMusicModel *model = self.musicArray[indexPath.row];
    if (model.isSelect) return;
    for (HRSelectBackGroundMusicModel *model in self.musicArray) {
        model.isSelect = NO;
    }
    model.isSelect = YES;
    [self.collectionView reloadData];
    if(self.selectMusicBlock){
        self.selectMusicBlock(model.musicName);
    }
}

-(void)showAnimation{
    WeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.left = 0;
    }];
}
- (void)hiddenAnimation{
    WeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.left = kFullWidth;
    }];
}

- (void)closeButtonClick{
    [self hiddenAnimation];
}

@end
